import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ZhongJinTao
 * Date: 2024-03-05
 * Time: 16:18
 */
public class IODemo12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件");
        String srcFileName = scanner.nextLine();
        System.out.println("请输入目的文件");
        String desFileName = scanner.nextLine();
        //判断源文件是否合法
        File srcFile = new File(srcFileName);
        if(!srcFile.isFile()) {
            System.out.println("源文件不合法");
            return;
        }
        //判断目的文件是否合法，目的文件可以没有，但是必须要有文件路径
        File desFile = new File(desFileName);
        if(!desFile.getParentFile().isDirectory()) {
            System.out.println("目的路径不合法");
            return;
        }
        //复制源文件，一个字节一个字节的读，再一个字节一个字节的写进目的文件
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(desFile)) {
            while (true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                outputStream.write(buffer, 0, n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
